package edu.stanford.prpl.app.prpltalk.android;

import java.util.Calendar;
import java.util.GregorianCalendar;


public class ChatMessage 
{
	/* The time this message was received in milliseconds */
	private long time;	
	
	/* The message content */
	private String messageContent;
	
	/* The message sender name */
	private String messageSenderName;
	
	/* The message sender email */
	private String messageSenderEmail;
	
	
	/**
	 * Copies a  session message.
	 * @param message the message to be copied
	 */
	public ChatMessage(ChatMessage message)
	{
		this.messageSenderEmail = new String(message.messageSenderEmail);
		this.messageSenderName = new String(message.messageSenderName);
		this.messageContent = new String(message.messageContent);
		this.time = message.time;
	}
	

	/**
	 * Instantiates a new chat message.
	 * 
	 * @param message the message content as a string
	 * @param senderName the message sender name
	 * @param senderEmail the message sender email
	 * @param timestamp the time in milliseconds the message arrives at
	 */
	public ChatMessage(String message, String senderName, 
			           String senderEmail, long timestamp)
	{
		this.time = timestamp;
		this.messageContent = message;
		this.messageSenderName = senderName;
		this.messageSenderEmail = senderEmail;
	}

	/**
	 * Gets the sender phone number.
	 * @return the sender phone number
	 */
	public String getSenderEmail()
	{
		return this.messageSenderEmail;
	}
	
	/**
	 * Gets the time this message arrives at
	 * @return the time
	 */
	public long getTime() 
	{
		return time;
	}

	/**
	 * Gets the message content.
	 * @return the message content
	 */
	public String getMessageContent()
	{
		return messageContent;
	}
	
	/**
	 * Gets the sender name. 
	 * @return the sender name
	 */
	public String getSenderName()
	{
		return messageSenderName;
	}

	/**
	 * Overrides Object.equals(). Two messages are considered equals if they have same senderName and content
	 * 
	 * @param o object to be compared
	 * @return true if equals, false otherwise
	 */

	public boolean equals(Object msgObj) 
	{
		if ( !(msgObj instanceof ChatMessage) )
			return false;

		ChatMessage msg = (ChatMessage) msgObj;
		return (msg.messageContent.equals(messageContent) && msg.messageSenderName.equals(messageSenderName));
	} 
	
	/**
	 * Gets the time this message was received as a string.
	 * @return the time received as string
	 */
	public String getTimeReceivedAsString()
	{
		Calendar calend = new GregorianCalendar();
		calend.setTimeInMillis(time);
		int hour = calend.get(Calendar.HOUR_OF_DAY);
		int minutes = calend.get(Calendar.MINUTE);
		return pad(hour) + ":" + pad(minutes);	
	}

	/**
	 * Formats date and time correctly 
	 * @return the time received as string
	 */
	private String pad(int c) 
	{
		return (c<10)?"0"+String.valueOf(c):String.valueOf(c);
	}
	
	/**
	 * Overrides Object.toString(). Provides a string 
	 * representation for a message
	 * 
	 * @return String representation of the message
	 */
	public String toString()
	{
		StringBuffer buffer = new StringBuffer();
		buffer.append("At ");
		buffer.append(getTimeReceivedAsString());
		buffer.append(" ");
		buffer.append(messageSenderName);
		buffer.append(" says: \n");
		buffer.append(messageContent);
		buffer.append("\n\n");
		return buffer.toString();
	}
}
